/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package General;

import B3D_Elements.B3D_Element;
import Files.Configuration;
import Files.Project;
import GUI.Dialogs.AssetBrowserDialog;
import GUI.Dialogs.ObserverDialog;
import GUI.Dialogs.SplashDialog;
import GUI.EditPanes.EditTaskPane;
import GUI.Editor_MainGUI.EditorWindow;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 *
 * @author David
 */
public class CurrentData
{

    private static Project project;
    private static Configuration configuration;
    private static EditorWindow editorWindow;
    private static AssetBrowserDialog assetBrowserDialog;
    private static File materialDirectory = new File("dat//mats");
    private static SplashDialog splashDialog;
    private static ArrayList<String> materialFiles = new ArrayList<String>();
    private static Float[] fieldOfView;
    private static ObserverDialog observerDialog;
    /**
     *
     */
    public static float CAM_VERY_SLOW = 10,
            /**
             *
             */
            CAM_SLOW = 50,
            /**
             *
             */
            CAM_DEFAULT = 100,
            /**
             *
             */
            CAM_FAST = 200,
            /**
             *
             */
            CAM_VERY_FAST = 400;
    private static boolean appRunning = false;

    /**
     *
     * @return
     */
    public static SplashDialog getSplashDialog()
    {
        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException ex)
        {
            CurrentData.getObserverDialog().printError("Thread.sleep in SplashDialog (CurrentData) interrupted", ex);
        }
        return splashDialog;
    }

    /**
     *
     * @param splashDialog
     */
    public static void setSplashDialog(SplashDialog splashDialog)
    {
        CurrentData.splashDialog = splashDialog;
    }

    /**
     *
     * @return
     */
    public static File getMaterialDirectory()
    {
        return materialDirectory;
    }

    /**
     *
     * @param materialDirectory
     */
    public static void setMaterialDirectory(File materialDirectory)
    {
        CurrentData.materialDirectory = materialDirectory;
    }

    /**
     *
     * @return
     */
    public static EditorWindow getEditorWindow()
    {
        return editorWindow;
    }
    static boolean doubleClickSortedOut = true;

    /**
     *
     * @param editorWindow
     */
    public static void setEditorWindow(EditorWindow editorWindow)
    {
        CurrentData.editorWindow = editorWindow;
    }

    /**
     *
     * @return
     */
    public static Project getProject()
    {
        return project;
    }

    /**
     *
     * @param proj
     */
    public static void setProject(Project proj)
    {
        CurrentData.project = proj;
    }

    /**
     *
     * @return
     */
    public static Configuration getConfiguration()
    {
        return configuration;
    }

    /**
     *
     * @param configuration
     */
    public static void setConfiguration(Configuration configuration)
    {
        CurrentData.configuration = configuration;
    }

    /**
     *
     * @return
     */
    public static AssetBrowserDialog getAssetBrowserDialog()
    {
        return assetBrowserDialog;
    }

    /**
     *
     * @param assetBrowserDialog
     */
    public static void setAssetBrowserDialog(AssetBrowserDialog assetBrowserDialog)
    {
        CurrentData.assetBrowserDialog = assetBrowserDialog;
    }

    /**
     *
     */
    public static void initProjectScene()
    {
        CurrentData.getEditorWindow().initNewScene(CurrentData.getProject().getWorld());
    }

    /**
     *
     * @return
     */
    public static List<String> getAllMaterialFiles()
    {
        return materialFiles;
    }

    static void huntMaterials()
    {
        /*Ich verstehe nicht mehr, was hier passiert. Klappt aber, gut gemacht, Vergangenheitsdavid*/
        Vector<String> paths = new Vector<String>();
        String libPath = System.getProperty("java.class.path");
        StringTokenizer st = null;
        if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0)
        {
            //if it's windows
            st = new StringTokenizer(libPath, ";");
        } else
        {
            //if it's linux/unix/?mac?
            st = new StringTokenizer(libPath, ":");
        }
        while (st.hasMoreTokens())
        {
            String next = st.nextToken();
            if (next.endsWith(".jar"))
            {
                paths.add(next);
            }
        }
        for (String s : paths)
        {
            URL jar = null;
            try
            {
                jar = new File(s).toURI().toURL();
            } catch (MalformedURLException ex)
            {
                CurrentData.getObserverDialog().printError("Error while hunting Materials\n\tPath -> " + s, ex);
            }
            ZipInputStream zis = null;
            try
            {
                zis = new ZipInputStream(jar.openStream());
            } catch (IOException ex)
            {
                CurrentData.getObserverDialog().printError("Error creating ZipInputStream", ex);
            }
            while (true)
            {
                ZipEntry entry = null;
                try
                {
                    entry = zis.getNextEntry();
                } catch (IOException ex)
                {
                    CurrentData.getObserverDialog().printError("Error getting next ZipEntry", ex);
                    break;
                }
                if (entry != null)
                {
                    String e = entry.toString();
                    if (e.endsWith(".j3md")
                            && !e.contains("ShowNormals")
                            && !e.contains("NiftyQuadGrad")
                            && !e.contains("SimpleSpriteParticle")
                            && !e.contains("PreShadow")
                            && !e.contains("PostShadow")
                            && !e.contains("tonegod"))
                    {
                        materialFiles.add(e);
                    }
                } else
                {
                    break;
                }
            }
        }
    }

    /**
     *
     * @return
     */
    public static boolean isAppRunning()
    {
        return appRunning;
    }

    /**
     *
     * @param appRunning
     */
    public static void setAppRunning(boolean appRunning)
    {
        CurrentData.appRunning = appRunning;
    }

    public static Float[] getFieldOfView()
    {
        return fieldOfView;
    }

    public static void setDefaultFieldOfView(Float[] fieldOfView)
    {
        CurrentData.fieldOfView = fieldOfView;
    }

    public static ObserverDialog getObserverDialog()
    {
        return observerDialog;
    }

    public static void setObserverDialog(ObserverDialog observerDialog)
    {
        CurrentData.observerDialog = observerDialog;
    }
}
